<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\ProjectMonths as ProjectMonthsModel;
use think\Request;

class Month extends Base
{
    public function monthlist(Request $request)
    {
        try {

            $CurrTime = time();
            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $uid = $request->param('uid');
            $ProjectMonthsModel = new ProjectMonthsModel;
            $list = $ProjectMonthsModel->alias('w')
                    ->field('w.id,w.uid,w.month,w.project_id,w.class_id,w.stage_id,w.main_content,
                    pro.project_no,pro.project_name,u.realname as project_pm,s.name as stage_name'
                    )
                    ->order('w.id','desc')
                    ->join('project_info pro','w.project_id = pro.id')
                    ->leftjoin('project_info_stage_route r','r.project_id = pro.id and r.class_id = pro.project_class and r.start_time < '.$CurrTime.' and end_time >'.$CurrTime)
                    ->leftjoin('user u','pro.project_pm = u.id')
                    ->leftjoin('project_stage s','r.stage_id = s.id')
                    ->where('u.id','=',$uid)
                    ->order('w.month desc')
                    ->paginate([
                        'list_rows'=> $pagesize,
                        'var_page' => 'page',
                        'page'=>$page
                    ])->each(function($item, $key){
                        $item['month'] = date("Y-m",$item['month']);
                        return $item;
                    });
            $result = ["status"=>true,"msg"=>"获取成功！","data"=>$list];
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    public function monthadd(Request $request)
    {
        try {

            $data = [];
            if($request->param('project_id')) $data['project_id'] = $request->param('project_id');
            if($request->param('class_id')) $data['class_id'] = $request->param('class_id');
            if($request->param('stage_id')) $data['stage_id'] = $request->param('stage_id');
            if($request->param('month')) $data['month'] = strtotime(date('Y-m',strtotime($request->param('month'))));
            if($request->param('main_content')) $data['main_content'] = $request->param('main_content');
            if($request->param('uid')) $data['uid'] = $request->param('uid');
            $data['create_time'] = time();

            $ProjectMonthsModel = new ProjectMonthsModel;
            $info = $ProjectMonthsModel->where('month','like',$data['month'])
                                       ->where('uid','=',$data['uid'])
                                       ->where('project_id','=',$data['project_id'])
                                       ->find();
            if($info){
                $result = ["status"=>false,"msg"=>"数据已经添加，请不要重复添加！"];
                return $result;
            }

            $re = $ProjectMonthsModel->insertGetId($data);

            if($re){
                $result = ["status"=>true,"re_id"=>$re,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"re_id"=>"","msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    public function monthdetail(Request $request){
        try {
            $id = $request->param('id');
            if($id){
                $ProjectMonthsModel = new ProjectMonthsModel;
                $detail = $ProjectMonthsModel->alias('w')
                                       ->field('w.id,w.uid,w.month,w.project_id,w.class_id,p.name as class_name,w.stage_id,w.main_content,i.project_no')
                                       ->leftjoin('project_class p','w.class_id = p.id')
                                       ->leftjoin('project_info i','w.project_id = i.id')
                                       ->where('w.id',$id)->find();

                $detail['month'] = strtotime($detail['month']);
                unset($detail['update_time']);
                unset($detail['create_time']);
                $result =  ["status"=>true,"msg"=>"获取成功！","data"=>$detail];
                return json($result);
            }else{
                $result = ["status"=>false,"msg"=>"参数必须"];
                return json($result);
            }

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    public function monthsave(Request $request)
    {
        try {

            $data = [];
            $id = $request->param('id');
            if($request->param('project_id')) $data['project_id'] = $request->param('project_id');
            if($request->param('class_id')) $data['class_id'] = $request->param('class_id');
            if($request->param('stage_id')) $data['stage_id'] = $request->param('stage_id');
            if($request->param('month')) $data['month'] = strtotime($request->param('month'));
            if($request->param('main_content')) $data['main_content'] = $request->param('main_content');
            if($request->param('uid')) $data['uid'] = $request->param('uid');
            $data['update_time'] = time();

            $ProjectMonthsModel = new ProjectMonthsModel;
            $re = $ProjectMonthsModel->where('id',$id)->save($data);

            if($re!==false) {
                $result = ["status"=>true,"re_id"=>$re,"msg"=>"更新成功！"];
            }else{
                $result = ["status"=>false,"re_id"=>"","msg"=>"更新失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    public function monthdel(Request $request)
    {
        try {
            $id = $request->param('id');
            $uid = $request->param('uid');
            if($id){

                $ProjectMonthsModel = new ProjectMonthsModel;
                $re = $ProjectMonthsModel->where('id',$id)->where('uid',$uid)->delete();
                if($re){
                    $result = ["status"=>true,"msg"=>"删除成功！"];
                    return json($result);
                }else{
                    $result = ["status"=>false,"msg"=>"删除失败！"];
                    return json($result);
                }

            }else{
                $result = ["status"=>false,"msg"=>"参数必须"];
                return json($result);
            }
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
